package net.hn.hnms.biz.indicator.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import net.hn.hnms.biz.indicator.entity.*;
import net.hn.hnms.biz.indicator.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * @author 智呈物联 nzw
 * @date 2022/9/21
 */
@RestController
@RequestMapping("safety/indicator")
@Api(value = "安全态势", tags = {"安全态势"})
public class IndicatorController {

    private SysIndicatorModelService sysIndicatorModelService;

    private SysIndicatorBaseDataService sysIndicatorBaseDataService;

    private SysIndicatorService sysIndicatorService;

    private DwsMineMathematicalModelService dwsMineMathematicalModelService;

    private SysIndicatorRolesService sysIndicatorRolesService;

    @Autowired
    public void setSysIndicatorRolesService(SysIndicatorRolesService sysIndicatorRolesService) {
        this.sysIndicatorRolesService = sysIndicatorRolesService;
    }

    @Autowired
    public void setSysIndicatorService(SysIndicatorService sysIndicatorService) {
        this.sysIndicatorService = sysIndicatorService;
    }

    @Autowired
    public void setDwsMineMathematicalModelService(DwsMineMathematicalModelService dwsMineMathematicalModelService) {
        this.dwsMineMathematicalModelService = dwsMineMathematicalModelService;
    }

    @Autowired
    public void setSysIndicatorModelService(SysIndicatorModelService sysIndicatorModelService) {
        this.sysIndicatorModelService = sysIndicatorModelService;
    }

    @Autowired
    public void setSysIndicatorBaseDataService(SysIndicatorBaseDataService sysIndicatorBaseDataService) {
        this.sysIndicatorBaseDataService = sysIndicatorBaseDataService;
    }

    @ApiOperation("指标模型详情")
    @GetMapping("/{id}")
    public ResponseEntity baseInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(sysIndicatorService.selectById(id));
    }


    @PostMapping("/add")
    @ApiOperation("指标模型添加")
    public ResponseEntity<SysIndicator> add(@RequestBody SysIndicator sysIndicator) {
        SysIndicator save = sysIndicatorService.saveInfo(sysIndicator);
        return new ResponseEntity<SysIndicator>(save, HttpStatus.OK);
    }

    @ApiOperation("指标修改")
    @PostMapping("/edit")
    public ResponseEntity<Boolean> edit(@RequestBody SysIndicator sysIndicator) {
//        if(true){
//            return doNothing();
//        }
        SysIndicator edit = sysIndicatorService.updateInfo(sysIndicator);
        return new ResponseEntity<Boolean>(edit != null, HttpStatus.OK);
    }

    private ResponseEntity doNothing() {
        return ResponseEntity.ok().build();
    }

    @ApiOperation("指标删除")
    @GetMapping("/remove")
    public ResponseEntity<Boolean> remove(Long id) {
//        if(true){
//            return doNothing();
//        }
        boolean edit = sysIndicatorService.removeById(id);
        return new ResponseEntity<Boolean>(edit, HttpStatus.OK);
    }

    @ApiOperation("删除规程")
    @GetMapping("/deleteRole")
    public ResponseEntity<Boolean> deleteRole(Long id) {
//        if(true){
//            return doNothing();
//        }
        Assert.notNull(id, "没有id");
        boolean save = sysIndicatorRolesService.removeById(id);
        return ResponseEntity.ok(save);
    }

    @ApiOperation(value = "获取指标模型分级数据", position = 2)
    @GetMapping("treeList")
    public ResponseEntity<Object> treeList(String name) {
        return ResponseEntity.ok(sysIndicatorModelService.treeList(name));
    }

    @ApiOperation("基础信息列表")
    @GetMapping("/base")
    public ResponseEntity base() {
        return ResponseEntity.ok(sysIndicatorBaseDataService.allList());
    }

    @ApiOperation("修改基础信息")
    @PostMapping("/base/update")
    public ResponseEntity baseUpdate(@RequestBody SysIndicatorBaseData sysIndicatorBaseData) {
        return ResponseEntity.ok(sysIndicatorBaseDataService.updateById(sysIndicatorBaseData));
    }

    @ApiOperation("灾害模型列表")
    @GetMapping("/mathematical/pageList")
    public R mathematical(PageQuery pageQuery, String modelName, String modelType) {
        return R.ok(dwsMineMathematicalModelService.pageList(pageQuery, modelName, modelType));
    }

    @ApiOperation("灾害模型元素列表")
    @GetMapping("mathematical/items")
    public ResponseEntity mathematicalItems(Long modelId) {
        return ResponseEntity.ok(dwsMineMathematicalModelService.listByModel(modelId));
    }

    @ApiOperation("添加或修改灾害模型元素")
    @PostMapping("mathematical/item/save")
    public ResponseEntity mathematicalItemsUpdate(@RequestBody MineMathematicalModelDTO mineMathematicalModelDTO) {
        dwsMineMathematicalModelService.update(mineMathematicalModelDTO);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("添加规程")
    @PostMapping("/addOrUpdateRole")
    public ResponseEntity<Boolean> addRole(@RequestBody SysIndicatorRoles indicatorRoles) {
//        if(true){
//            return doNothing();
//        }
        Assert.notNull(indicatorRoles.getIndicatorId(), "没有指定指标模型");
        Assert.hasText(indicatorRoles.getTitle(), "标题不能为空");
        Assert.hasText(indicatorRoles.getContent(), "内容不能为空");
        if (Objects.isNull(indicatorRoles.getId())) {
            indicatorRoles.setCreateTime(new Date());
        }
        boolean save = sysIndicatorService.saveOrUpdate(indicatorRoles);
        return ResponseEntity.ok(save);
    }

    @ApiOperation(value = "编辑获取详情", position = 1)
    @GetMapping("info")
    public ResponseEntity<Object> getInfo(Long id) {
        return ResponseEntity.ok(sysIndicatorService.getInfo(id));
    }


    @ApiOperation("指标模型结果分析")
    @GetMapping("/analyse")
    @AutoSetMineCode
    public R<Map> analyse(
            String type
            , String mineCode) throws Exception {
        Map analyse = sysIndicatorModelService.analyse(type, mineCode);
        return R.ok(analyse);
    }

    @ApiOperation(value = "获取指标分布")
    @GetMapping("/queryModelList")
    @AutoSetMineCode
    public ResponseEntity<Object> queryModelList(String mineCode) {
        return ResponseEntity.ok(sysIndicatorModelService.queryModelList(mineCode));
    }

    @ApiOperation(value = "获取指标分布对比(和前一天比较)", position = 4)
    @GetMapping("/queryModelListCompare")
    @AutoSetMineCode
    public ResponseEntity<Object> queryModelListCompare(String mineCode) throws Exception {
        return ResponseEntity.ok(sysIndicatorModelService.queryModelListCompare(mineCode));
    }

    @ApiOperation("风险预测")
    @GetMapping("/curve")
    public ResponseEntity<Map> curve(String type) {
        Map curve = sysIndicatorModelService.curve(type);
        return ResponseEntity.ok(curve);
    }

    @GetMapping("/calculateTotalScore")
    @ApiOperation("各矿安全态势最新评分")
    public R<Object> calculateTotalScore() {
        return R.ok(sysIndicatorModelService.calculateTotalScore());
    }

    @GetMapping("/getScore")
    @ApiOperation("安全态势各类评分情况")
    public R<ScoreParentVO> getScore(String type) {
        return R.ok(sysIndicatorModelService.getScore(type));
    }

}
